﻿#include "menuform.h"
#include "ui_menuform.h"

MenuForm::MenuForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MenuForm)
{
    ui->setupUi(this);


    init();

    connect(ui->m_btnAdd, &QPushButton::clicked, this, &MenuForm::on_add);
    connect(ui->m_btnEdit, &QPushButton::clicked, this, &MenuForm::on_edit);
    connect(ui->m_btnDelete, &QPushButton::clicked, this, &MenuForm::on_delete);

    connect(ui->m_btnQuery, &QPushButton::clicked, this, &MenuForm::init);
}

MenuForm::~MenuForm()
{
    delete ui;
}

void MenuForm::init()
{
    if(ui->m_treeViewMenu->model())
    {
        qDebug() << "delete";
        delete ui->m_treeViewMenu->model();
    }

    //1，构造Model，这里示例具有3层关系的model构造过程
    m_pStandardItemModel = new QStandardItemModel(ui->m_treeViewMenu);
    m_pStandardItemModel->setHorizontalHeaderLabels(QStringList()
        << QStringLiteral("菜单ID")
        << QStringLiteral("菜单名")
        << QStringLiteral("菜单栏")
        << QStringLiteral("工具栏"));     //设置列头

    QSqlDatabase database = QSqlDatabase::database(DbUtil::SQLITE_CONNECTION_NAME, true);
    if(database.isOpen())
    {
        qDebug() << "open";

        QSqlQuery sql_query(database);

        QString select_sql = "select menu_id, menu_name, pmenu_id, is_menubar, is_toolbar "
                             "from t_menu where pmenu_id = 0";
        if(!sql_query.exec(select_sql))
        {
            qDebug() << sql_query.lastError().text();
        }
        else
        {
            while(sql_query.next())
            {
                //一级节点，加入mModel
                QList<QStandardItem*> rows;
                QStandardItem* item1 = new QStandardItem(QString::number(sql_query.value(0).toLongLong()));
                QStandardItem* item2 = new QStandardItem(sql_query.value(1).toString());
                QStandardItem* item3 = new QStandardItem(QString::number(sql_query.value(3).toInt()));
                QStandardItem* item4 = new QStandardItem(QString::number(sql_query.value(4).toInt()));
                rows.append(item1);
                rows.append(item2);
                rows.append(item3);
                rows.append(item4);
                m_pStandardItemModel->appendRow(rows);

                QSqlQuery sql_query1(database);

                select_sql = QString("select menu_id, menu_name, pmenu_id, is_menubar, is_toolbar "
                                             "from t_menu where pmenu_id = %1 and menu_name like '%%2%'")
                        .arg(sql_query.value(0).toLongLong())
                        .arg(ui->m_menuName->text());
                if(!sql_query1.exec(select_sql))
                {
                    qDebug() << sql_query1.lastError().text();
                }
                else
                {
                    while(sql_query1.next())
                    {
                        //二级节点,加入第1个一级节点
                        QList<QStandardItem*> subRows;
                        QStandardItem* subItem1 = new QStandardItem(QString::number(sql_query1.value(0).toLongLong()));
                        QStandardItem* subItem2 = new QStandardItem(sql_query1.value(1).toString());
                        QStandardItem* subItem3 = new QStandardItem(QString::number(sql_query1.value(3).toInt()));
                        QStandardItem* subItem4 = new QStandardItem(QString::number(sql_query1.value(4).toInt()));
                        subRows.append(subItem1);
                        subRows.append(subItem2);
                        subRows.append(subItem3);
                        subRows.append(subItem4);

                        item1->appendRow(subRows);
                    }
                }
            }
        }
    }

    //2，给QTreeView应用model
    ui->m_treeViewMenu->setModel(m_pStandardItemModel);
}

void MenuForm::on_add()
{
    Menu menu;
    menu.op = "ADD";

    QModelIndex selected = ui->m_treeViewMenu->currentIndex(); //选中的行

    if(selected.isValid())
    {
        QModelIndex item = selected.sibling(selected.row(), 0);
        menu.pMenuId = ui->m_treeViewMenu->model()->itemData(item)[0].toLongLong();

    }
    else
    {
       menu.pMenuId = 0;
    }


    MenuDialog menuDialog;
    menuDialog.init(menu);
    if(menuDialog.exec() == QDialog::Accepted)
    {
        init();
    }
}

void MenuForm::on_edit()
{
    Menu menu;
    menu.op = "EDIT";

    QModelIndex selected = ui->m_treeViewMenu->currentIndex(); //选中的行

    if(selected.isValid())
    {
        QModelIndex item = selected.sibling(selected.row(), 0);
        menu.menuId = ui->m_treeViewMenu->model()->itemData(item)[0].toLongLong();
        item = selected.sibling(selected.row(), 1);
        menu.menuName = ui->m_treeViewMenu->model()->itemData(item)[0].toString();
        item = selected.sibling(selected.row(), 2);
        menu.isMenuBar = static_cast<qint8>(ui->m_treeViewMenu->model()->itemData(item)[0].toInt());
        item = selected.sibling(selected.row(), 3);
        menu.isToolBar = static_cast<qint8>(ui->m_treeViewMenu->model()->itemData(item)[0].toInt());
    }
    else
    {
        QMessageBox::information(this,
                                 QString::fromLocal8Bit("提示"),
                                 QString::fromLocal8Bit("请选择修改项"));
       return;
    }

    MenuDialog menuDialog;
    menuDialog.init(menu);
    if(menuDialog.exec() == QDialog::Accepted)
    {
        init();
    }
}

void MenuForm::on_delete()
{
    QModelIndex selected = ui->m_treeViewMenu->currentIndex(); //选中的行

    if(!selected.isValid())
    {
        QMessageBox::information(this,
                                 QString::fromLocal8Bit("提示"),
                                 QString::fromLocal8Bit("请选择删除项"));
        return;
    }

    if(QMessageBox::question(this,
                             QString::fromLocal8Bit("提示"),
                             QString::fromLocal8Bit("请确认删除"),
                             QString::fromLocal8Bit("取消"),
                             QString::fromLocal8Bit("确认")) != 1)
    {
        return;
    }

    QModelIndex item = selected.sibling(selected.row(), 0);
    qint64 menuId = ui->m_treeViewMenu->model()->itemData(item)[0].toLongLong();

    QString sql = QString("select count(menu_id) from t_menu where pmenu_id = %1")
            .arg(menuId);
    QSqlDatabase database = QSqlDatabase::database(DbUtil::SQLITE_CONNECTION_NAME, true);
    if (database.open())
    {
        QSqlQuery sql_query(database);
        if(sql_query.exec(sql))
        {
            qDebug() << "count t_menu" << sql;
            if(sql_query.next())
            {
               int count = sql_query.value(0).toInt();
               if(count == 0)
               {
                   sql = QString("delete from t_menu where menu_id = %1")
                           .arg(menuId);
                   if(sql_query.exec(sql))
                   {
                       init();
                       qDebug() << "delete t_menu" << sql;
                   }
                   else
                   {
                       qDebug() << sql_query.lastError().text();
                   }
               }
               else
               {
                   QMessageBox::information(this,
                                            QString::fromLocal8Bit("提示"),
                                            QString::fromLocal8Bit("请先删除子节点"));
                   return;
               }
            }
            else
            {
                qDebug() << sql_query.lastError().text();
            }
        }
    }
}
